package com.elong.android.specialhouse.adapter.common;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @interfaceName: BasePowerAdapter
 * @Description: Adapter基类，抽象出共性的convertView处理和数据处理
 * @author kaiqi.li
 * @date 2015-7-28 下午4:2:37
 */
public abstract class BasePowerAdapter<T, H extends BaseViewHolder> extends
		BaseAdapter {

	protected static final String TAG = BasePowerAdapter.class.getSimpleName();
	protected final Context mContext;
	protected List<T> mDatas;
	protected int mItemLayoutId;
	protected boolean displayIndeterminateProgress = false; // 是否在ListView底部展示ProgressBar
	protected MultiItemType<T> mMultiItemType;

	public BasePowerAdapter(Context context, int layoutId) {

		this(context, layoutId, null);
	}

	public BasePowerAdapter(Context context, int layoutId, List<T> datas) {
		this.mContext = context;
		this.mItemLayoutId = layoutId;
		this.mDatas = datas == null ? new ArrayList<T>()
				: new ArrayList<T>(datas);
	}

	public BasePowerAdapter(Context context, List<T> datas,
			MultiItemType<T> multiItemType) {
		this.mContext = context;
		this.mDatas = datas == null ? new ArrayList<T>() : new ArrayList<T>(
				datas);
		this.mMultiItemType = multiItemType;
	}

	@Override
	public int getCount() {

		int extra = displayIndeterminateProgress ? 1 : 0;
		return mDatas.size() + extra;
	}

	@Override
	public T getItem(int position) {
		if (position >= mDatas.size()) {
			return null;
		}
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public int getItemViewType(int position) {

		if (displayIndeterminateProgress) {
			if (mMultiItemType != null) {
				return position >= mDatas.size() ? 0 : mMultiItemType
						.getItemViewType(position, mDatas.get(position));
			}
		} else {
			if (mMultiItemType != null) {
				mMultiItemType.getItemViewType(position, mDatas.get(position));
			}
		}
		return position >= mDatas.size() ? 0 : 1;
	}

	@Override
	public int getViewTypeCount() {
		if (mMultiItemType != null) {
			return mMultiItemType.getViewTypeCount() + 1;
		}
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// 显示ProgressBar
		if (getItemViewType(position) == 0) {
			createIndeterminateProgressView(convertView, parent);
		}
		final H holder = getAdapterHolder(position, convertView, parent);
		T item = getItem(position);
		holder.setAssociatedObject(item);
		convert(holder, item);
		return holder.getConvertView();
	}

	@Override
	public boolean isEnabled(int position) {

		return position >= mDatas.size() ? false : true;
	}

	protected abstract void convert(H holder, T item);

	private View createIndeterminateProgressView(View convertView,
			ViewGroup parent) {
		if (convertView == null) {
			FrameLayout container = new FrameLayout(mContext);
			container.setForegroundGravity(Gravity.CENTER);
			ProgressBar progress = new ProgressBar(mContext);
			container.addView(progress);
			convertView = container;
		}
		return convertView;
	}

	public abstract H getAdapterHolder(int position, View convertView,
			ViewGroup parent);

	public void add(T elem) {
		mDatas.add(elem);
		notifyDataSetChanged();
	}

	public void addAll(List<T> elem) {
		mDatas.addAll(elem);
		notifyDataSetChanged();
	}
	public void addAll(int position,List<T> elem) {
		mDatas.addAll(position,elem);
		notifyDataSetChanged();
	}

	public void set(T oldElem, T newElem) {
		set(mDatas.indexOf(oldElem), newElem);
	}

	public void set(int index, T elem) {
		mDatas.set(index, elem);
		notifyDataSetChanged();
	}

	public void remove(T elem) {
		mDatas.remove(elem);
		notifyDataSetChanged();
	}

	public void remove(int index) {
		mDatas.remove(index);
		notifyDataSetChanged();
	}

	public void replaceAll(List<T> elem) {
		if (elem != null && elem.size() > 0) {
			mDatas.clear();
			mDatas.addAll(elem);
			notifyDataSetChanged();
		}

	}

	public boolean contains(T elem) {
		return mDatas.contains(elem);
	}

	/** Clear data list */
	public void clear() {
		mDatas.clear();
		notifyDataSetChanged();
	}

	//设置是否显示ProgressBar
	public void showIndeterminateProgress(boolean display) {
		if (display == displayIndeterminateProgress)
			return;
		displayIndeterminateProgress = display;
		notifyDataSetChanged();
	}
}
